package com.bvgol.example.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class MyRabbitMQInitConfig {

    /* 容器中的Queue、Exchange、Binding 会自动创建（在RabbitMQ）不存在的情况下 */
    /* 如果存在,参数发生变化,mq中不会变化*/




    /**
     * 基础队列
     * 此种方式可以创建队列,绑定,交换机
     * 此种方式可以初始化队列,绑定,交换机 更换环境的时候方便一些
     */
    @Bean
    public Queue helloJavaQueue(){
        return new Queue("hello-java-queue");
    }
    @Bean
    public Exchange helloJavaExchange(){
        return new DirectExchange("hello-java-exchange");
    }
    @Bean
    public Binding helloJavaBinding(){
       return new Binding("hello-java-queue",
                Binding.DestinationType.QUEUE,
                "hello-java-exchange",
                "hello.java",
                null);
    }



    /**
     * 死信队列
     *
     * @return
     */@Bean
    public Queue orderDelayQueue() {
        /*
            Queue(String name,  队列名字
            boolean durable,  是否持久化
            boolean exclusive,  是否排他
            boolean autoDelete, 是否自动删除
            Map<String, Object> arguments) 属性

         */
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        //map 中key是没有校验的``写错了就不会在web中Features列中展示对应参数  会新增一个aggs
        arguments.put("x-message-ttl", 60000); // 消息过期时间 单位是毫秒 60000ms == 1min
        Queue queue = new Queue("demo.order.delay.queue", true, false, false, arguments);
        return queue;
    }

    /**
     * 普通队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {
        Queue queue = new Queue("demo.order.release.order.queue", true, false, false);
        return queue;
    }


    /**
     * TopicExchange
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        /*
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         * */
        return new TopicExchange("demo.order-event-exchange", true, false);
    }


    @Bean
    public Binding orderCreateOrderBinding() {
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding("demo.order.delay.queue",
                Binding.DestinationType.QUEUE,
                "demo.order-event-exchange",
                "demo.order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding("demo.order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "demo.order-event-exchange",
                "demo.order.release.order",
                null);
    }
}
